/**
 * 当前日期 yyyy-MM-dd
 */
export function getNowDate() {
    let date = new Date();
    let seperator1 = "-";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

/**
 * 当前月份 yyyy-MM
 */
export function getNowMonth() {
    let date = new Date();
    let seperator1 = "-";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    let currentdate = year + seperator1 + month;
    return currentdate;
}

/**
 * 当前时间-day yyyy-MM-dd
 * @param {Object} day
 */
export function DateMinus(day) {
    let nowdate = new Date();
    let computeDate, computeYear, computeMonth, computeDay;
    if (day !== 30) {
        computeDate = new Date(nowdate - day * 24 * 3600 * 1000);
        computeYear = computeDate.getFullYear();
        computeMonth = computeDate.getMonth() + 1;
        computeDay = computeDate.getDate();
    } else {
        computeYear = nowdate.getFullYear();
        computeMonth = nowdate.getMonth();
        computeDay = nowdate.getDate();
    }
    if (computeMonth >= 1 && computeMonth <= 9) {
        computeMonth = "0" + computeMonth;
    }
    if (computeDay >= 1 && computeDay <= 9) {
        computeDay = "0" + computeDay;
    }

    let currentdate = computeYear + '-' + computeMonth + '-' + computeDay;
    return currentdate;
}

/**
 * 当前月份-data yyyy-MM
 * @param {Object} data
 */
export function getMonthMinus(data) {
    let nowdate = new Date();
    let computeDate, computeYear, computeMonth, computeDay;
    computeDate = new Date(nowdate - data * 24 * 3600 * 1000 * 30);
    computeYear = computeDate.getFullYear();
    computeMonth = computeDate.getMonth() + 1;
    if (computeMonth >= 1 && computeMonth <= 9) {
        computeMonth = "0" + computeMonth;
    }

    let currentdate = computeYear + '-' + computeMonth;
    return currentdate;
}

/**
 * 当前时间 yyyy-MM-dd HH:MM:SS
 */
export function getNowFormatDate() {
    let date = new Date();
    let seperator1 = "-";
    let seperator2 = ":";
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    let seconds = date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + seconds;
    return currentdate;
}

/**
 * 获取当前月第一天yyyy-MM-dd
 */
export function getNowMonthFirstDay() {
    var date = new Date();
    let seperator1 = "-";

    let month = date.getMonth() + 1;

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }

    return date.getFullYear() + seperator1 + month + seperator1 + "01";
}

/**
 * 获取当前月最后一天yyyy-MM-dd
 */
export function getNowMonthLastDay() {
    var date = new Date();
    let seperator1 = "-";

    let month = date.getMonth() + 1;

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }

    let endOfMonth = new Date(date.getFullYear(), month, 0).getDate(); // 获取本月最后一天

    return date.getFullYear() + seperator1 + month + seperator1 + endOfMonth;
}

/**
 * 获取本周第一天yyyy-MM-dd
 */
export function getNowWeekFirstDay() {
    var date = new Date();
    var yy = date.getFullYear();
    var mm = date.getMonth() + 1;
    var dd = date.getDate();
    var ww = ((date.getDay() != 0) ? date.getDay() : 7);

    var nd = date.valueOf() - (ww - 1) * 24 * 60 * 60 * 1000;
    nd = new Date(nd);

    let seperator1 = "-";
    let month = nd.getMonth() + 1;
    let strDate = nd.getDate();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    return yy + seperator1 + month + seperator1 + strDate;
}

/**
 * 获取本周最后一天yyyy-MM-dd
 */
export function getNowWeekLastDay() {
    var date = new Date();
    var yy = date.getFullYear();
    var mm = date.getMonth() + 1;
    var dd = date.getDate();
    var ww = ((date.getDay() != 0) ? date.getDay() : 7);

    var nd = date.valueOf() + (7 - ww) * 24 * 60 * 60 * 1000;
    nd = new Date(nd);

    let seperator1 = "-";
    let month = nd.getMonth() + 1;
    let strDate = nd.getDate();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    return yy + seperator1 + month + seperator1 + strDate;
}

/**
 * 获取近三个月的开始那一天
 * @returns {string}
 */
export function getNearThreeMonthDate() {
    let date = new Date();
    let seperator1 = "-";
    let year = date.getFullYear();
    let month = date.getMonth() - 2;
    if (month <= 0) {
        month += 12;
        year -= 1;
    }
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

/**
 * 获取近一年的开始那一天
 * @returns {string}
 */
export function getNearYearDate() {
    let date = new Date();
    let seperator1 = "-";
    let year = date.getFullYear() - 1;
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}


// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 // 月份
        "d+": this.getDate(),                    // 日
        "h+": this.getHours(),                   // 小时
        "m+": this.getMinutes(),                 // 分
        "s+": this.getSeconds(),                 // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds()             // 毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

export function formatTimeToStr(times, pattern) {
    var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
    if (pattern) {
        d = new Date(times).Format(pattern);
    }
    return d.toLocaleString();
}

/**
 * 获取两个日期相差天数
 * @param {compareDate} 基准日期
 * @param {targetDate} 指定日期
 * @returns {string}
 */
export function getDaysBetween(compareDate, targetDate) {
    //sDate2是xxxx-xx-xx格式
    let date2 = new Date(targetDate);
    let date1 = new Date(compareDate);
    date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
    date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
    const diff = date2.getTime() - date1.getTime();
    const diffDate = diff / (24 * 60 * 60 * 1000);
    return diffDate;
}

/**
 * 获取某个时间相隔day天的日期 开始时间，往后加
 * @param {Object} fromDate 开始时间，默认不填即为当天
 * @param {Object} day 相隔多少天
 */
export function getDay(fromDate, day) {
    var today = new Date();
    if (fromDate) {
        today = new Date(fromDate);
    }
    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds); // 注意，这行是关键代码
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

/**
 * 获取某个时间相隔day天的日期 开始时间，往前数
 * @param {Object} fromDate 开始时间，默认不填即为当天
 * @param {Object} day 相隔多少天
 */
export function getDayBefore(fromDate, day) {
    var today = new Date();
    if (fromDate) {
        today = new Date(fromDate);
    }
    var targetday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds); // 注意，这行是关键代码
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

export function doHandleMonth(month) {
    var m = month;
    if (month.toString().length == 1) {
        m = "0" + month;
    }
    return m;
}

/**
 * 获取两个日期相差天数
 * @param {compareDate} 基准日期
 * @param {targetDate} 指定日期
 * @returns {string}
 */
export function getDaysOrHoursBetween(compareDate, targetDate) {
    //sDate2是xxxx-xx-xx格式
    let date2 = new Date(targetDate);
    let date1 = new Date(compareDate);
    const diff = date2.getTime() - date1.getTime();
    let diffDate = "";
    if (diff >= (24 * 60 * 60 * 1000)) {
        diffDate = Math.round(diff / (24 * 60 * 60 * 1000)) + "天";
    } else if (diff >= (60 * 60 * 1000)) {
        diffDate = Math.round(diff / (60 * 60 * 1000)) + "小时";
    } else {
        diffDate = Math.round(diff / (60 * 1000)) + "分钟";
    }
    return diffDate;
}



export default {
    getNowDate,
    getNowMonth,
    DateMinus,
    getNowFormatDate,
    getMonthMinus,
    getNearYearDate,
    getNowWeekFirstDay,
    getNowMonthFirstDay,
    formatTimeToStr,
    getNowMonthLastDay,
    getNowWeekLastDay,
    getDaysBetween,
    getDay,
    getDayBefore,
    doHandleMonth,
    getDaysOrHoursBetween,
}
